<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Http\Requests\CompanyRequest;
use Cache;
use DefaultCompany;

class CompanyController extends Controller
{

    /**
     * @api {get} /company/ 列表
     * @apiDescription 公司列表
     * @apiGroup 公司
     * @apiPermission none
     * @apiParam {String} Co_Name 公司名 
     * @apiParam {Number} pageSize 每页显示条数 
     * @apiParam {date} created_at  创建时间
     * @apiSuccessExample {json} 成功:
     *     HTTP/1.1 201
     *     {
     *         "code": 0,
     *         "message": "",
     *          "data": [
     *             {

     *             }
     *          ]
     *     }
     * @apiErrorExample {json} 失败:
     *     HTTP/1.1 422
     *     {
     *       "code": -1,
     *       "message": "错误提示"
     *     }
     */
    public function index(Request $request)
    {
        $perPage = $request->input('pageSize',15);
        $name = $request->input('Co_Name', '');
        $query = Company::query();
        if(!empty($name)) {
            $query->where('Co_Name', 'like', $name.'%');
        }
        $created = $request->input('created_at', []);
        if(!empty($created)) {
            $query->whereBetween('created_at', $created);
        }

        $list = $query->paginate($perPage);
        return $list;
    }




    /**
     * @api {post} /compnay/ 添加
     * @apiDescription 添加公司
     * @apiGroup 公司
     * @apiPermission none
     * @apiSuccessExample {json} 成功:
     *     HTTP/1.1 201
     *     {
     *         "code": 0,
     *         "message": "",
     *              
     *     }
     * @apiErrorExample {json} 失败:
     *     HTTP/1.1 422
     *     {
     *       "code": -1,
     *       "message": "添加失败"
     *     }
     */
    public function store(CompanyRequest $request)
    {
        $data = $request->input();
        
        $ret = Company::create($data);
        if(!$ret){
            $this->error('添加失败');
        }
		$this->setDefaultCompany($request, $ret);
    }


    public function show($id)
    {
        
    }


    /**
     * @api {put} /company/:id 更新
     * @apiDescription 更新公司
     * @apiGroup 公司
     * @apiPermission none
     * @apiParam {Number} id 公司编号
     * @apiSuccessExample {json} 成功:
     *     HTTP/1.1 201
     *     {
     *         "code": 0,
     *         "message": "",
     *              
     *     }
     * @apiErrorExample {json} 失败:
     *     HTTP/1.1 422
     *     {
     *       "code": -1,
     *       "message": "保存失败"
     *     }
     */
    public function update(CompanyRequest $request, $id)
    {
        $data = $request->input();
         
        $company = Company::find($id);
		
        $ret = $company->update($data);
		
        if(!$ret){
            $this->error('保存失败');
        }
		$this->setDefaultCompany($request, $company);
    }
	
	//设置默认公司
	protected function setDefaultCompany($request, $data) {
        $isDefault = $request->is_default;
        $result=$data->toArray();
        if(!$isDefault) {
            $result=null;
        }
        DefaultCompany::cacheCompanyData($result);
	}

    /**
     * @api {delete} /company/:id 删除
     * @apiDescription 删除公司
     * @apiGroup 公司
     * @apiPermission none
     * @apiParam {Number} id 公司编号
     * @apiSuccessExample {json} 成功:
     *     HTTP/1.1 201
     *     {
     *         
     *          "code": 0,
     *          "message": "",
     *        
     *     }
     * @apiErrorExample {json} 失败:
     *     HTTP/1.1 422
     *     {
     *       "code": -1,
     *       "message": "错误提示"
     *     }
     */
    public function destroy($id)
    {
        $company = Company::findOrFail($id);
        if(!$company->delete()){
            $this->error('删除失败');
        }
    }
    /**
     * @api {get} /company/:name/search 搜索公司
     * @apiDescription 按名称搜索公司
     * @apiGroup 公司
     * @apiPermission none
     * @apiParam {String} name 公司名称
     * @apiSuccessExample {json} 成功:
     *     HTTP/1.1 201
     *     {
     *         
     *          "code": 0,
     *          "message": "",
     *        
     *     }
     * @apiErrorExample {json} 失败:
     *     HTTP/1.1 422
     *     {
     *       "code": -1,
     *       "message": "错误提示"
     *     }
     */
    public function search(Request $request, $name)
    {
        $query = Company::query();
        $company = $query->where('Co_Name', 'like', trim($name).'%')
            ->select('Co_ID as value', 'Co_Name as label')
            ->get();
        return ['data'=>$company];
    }
}
